package com.dao.Impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.dao.BaseDao;
import com.dao.MvDao;
import com.entity.Mv;
import com.entity.Data.PagerData;
import com.entity.Query.MvQuery;
import com.util.MyUtil;

@Repository
public class MvDaoImpl extends BaseDao implements MvDao{

	@Override
	public List<Map<String, Object>> queryMv() {
		String sql="select m.mvId,m.mvName,m.mvMp4,m.mvPhoto,mt.mvTypeName,s.singerName "
				+ "from mv as m join mvtype as mt on m.mvTypeId=mt.mvTypeId "
				+ "join singer as s on m.singerId=s.singerId";
		return executeQuery(sql, null);
	}

	@Override
	public Map<String, Object> queryMusicId(int mvId) {
		String sql="select m.mvId,m.mvName,m.mvMp4,m.mvPhoto,m.mvTypeId,mt.mvTypeName,s.singerId,s.singerName "
				+ "from mv as m join mvtype as mt on m.mvTypeId=mt.mvTypeId "
				+ "join singer as s on m.singerId=s.singerId where mvId=?";
		Object[] params={mvId};
		return executeQueryMap(sql, params);
	}

	@Override
	public int addMv(Mv mv) {
		String sql="insert into mv(mvName,mvMp4,mvPhoto,mvTypeId,singerId)values(?,?,?,?,?)";
		Object[] params={mv.getMvName(),mv.getMvMp4(),mv.getMvPhoto(),mv.getMvTypeId(),mv.getSingerId()};
		return executeUpdate(sql, params);
	}

	@Override
	public int editMv(Mv mv) {
		String sql="update mv 	set mvName=?,mvTypeId=?,singerId=?	where mvId=?";
		Object[] params={mv.getMvName(),mv.getMvTypeId(),mv.getSingerId(),mv.getMvId()};
		return executeUpdate(sql, params);
	}

	@Override
	public int delMv(int mvId) {
		String sql ="delete from mv where mvId=?";
		Object[] params={mvId};
		return executeUpdate(sql, params);
	}

	@Override
	public PagerData queryByPage(MvQuery mvQuery, int curPage, int pageSize) {
		//根据条件拼接where语句
		String sqlWhere = "";
		List<Object> params = new ArrayList<Object>();
		//mv名字条件不为空
		if(!MyUtil.isNull(mvQuery.getMvName())){
			sqlWhere += " and m.mvName like ? ";
			params.add(mvQuery.getMvName());
		}
		//mv类型条件不为空
		if(mvQuery.getMvTypeId()!=null){
			sqlWhere += " and mt.mvTypeId = ? ";
			params.add(mvQuery.getMvTypeId());
		}
		//歌手条件不为空
		if(mvQuery.getSingerId()!=null){
			sqlWhere += " and s.singerId = ? ";
			params.add(mvQuery.getSingerId());
		}
		String sqlTotal ="select count(*) "
				+ " from mv as m join mvtype as mt on m.mvTypeId=mt.mvTypeId "
				+ " join singer as s on m.singerId=s.singerId"
				+ " where 1=1 " +sqlWhere;
		int totalCount = jdbcTemplate.queryForInt(sqlTotal,params.toArray());
		//根据总记录数获取总页数
		int totalPage = totalCount%pageSize==0 ?
				        totalCount/pageSize    :
				        totalCount/pageSize+1;
		String sql="select m.mvId,m.mvName,m.mvMp4,m.mvPhoto,mt.mvTypeName,s.singerName "
				+ " from mv as m join mvtype as mt on m.mvTypeId=mt.mvTypeId "
				+ " join singer as s on m.singerId=s.singerId "
				+ "	where 1=1 "+sqlWhere
				+ " order by m.mvId"
				+ " limit ?,?";
		//计算分页查询的开始位置
		int start = (curPage-1)*pageSize;
		
		params.add(start);
		params.add(pageSize);
		List<Map<String,Object>> dataList = executeQuery(sql,params.toArray());
		
		//封装分页数据对象
		PagerData pd = new PagerData();
		//当前第几页
		pd.setCurPage(curPage);
		//每页几条
		pd.setPageSize(pageSize);
		//总记录数
		pd.setTotalCount(totalCount);
		//总页数
		pd.setTotalPage(totalPage);
		//当前页数据记录
		pd.setDataList(dataList);
		return pd;
	}

}
